
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
	/// <summary>
	/// Sys_StatusRecord Data Class
	/// </summary>
	public partial class Sys_StatusRecordDAL : BaseDAL
	{
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
		public string TableName = "Sys_StatusRecord";

		public Sys_StatusRecordDAL()
		{ }

		#region  成员方法
		/// <summary>
		///  新增一条数据
		/// </summary>
		public bool Add(Sys_StatusRecord model)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("insert into [Sys_StatusRecord](");
			strSql.Append("[RecordId],[MemberAccount],[RecordTable],[RecordType],[BeforeValue],[AfterValue],[CreateTime],[Creator],[Memo],[ObjectID],[LoginID]");
			strSql.Append(") values (");
			strSql.Append("@RecordId,@MemberAccount,@RecordTable,@RecordType,@BeforeValue,@AfterValue,@CreateTime,@Creator,@Memo,@ObjectID,@LoginID");
			strSql.Append(")");

			SqlParameter[] parameters = {
				  
					new SqlParameter("@RecordId", model.RecordId),
					new SqlParameter("@MemberAccount", model.MemberAccount),
					new SqlParameter("@RecordTable", model.RecordTable),
					new SqlParameter("@RecordType", model.RecordType),
					new SqlParameter("@BeforeValue", model.BeforeValue),
					new SqlParameter("@AfterValue", model.AfterValue),
					new SqlParameter("@CreateTime", model.CreateTime),
					new SqlParameter("@Creator", model.Creator),
					new SqlParameter("@Memo", model.Memo),
					new SqlParameter("@ObjectID", model.ObjectID),
					new SqlParameter("@LoginID", model.LoginID)
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  保存一条数据
		/// </summary>
		public bool Update(Sys_StatusRecord model)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("update [Sys_StatusRecord] set ");
			strSql.Append("[MemberAccount]=@MemberAccount,[RecordTable]=@RecordTable,[RecordType]=@RecordType,[BeforeValue]=@BeforeValue,[AfterValue]=@AfterValue,[CreateTime]=@CreateTime,[Creator]=@Creator,[Memo]=@Memo,[ObjectID]=@ObjectID,[LoginID]=@LoginID");
			strSql.Append(" where [RecordId]=@RecordId");

			SqlParameter[] parameters = {
				  
					new SqlParameter("@RecordId", model.RecordId),
					new SqlParameter("@MemberAccount", model.MemberAccount),
					new SqlParameter("@RecordTable", model.RecordTable),
					new SqlParameter("@RecordType", model.RecordType),
					new SqlParameter("@BeforeValue", model.BeforeValue),
					new SqlParameter("@AfterValue", model.AfterValue),
					new SqlParameter("@CreateTime", model.CreateTime),
					new SqlParameter("@Creator", model.Creator),
					new SqlParameter("@Memo", model.Memo),
					new SqlParameter("@ObjectID", model.ObjectID),
					new SqlParameter("@LoginID", model.LoginID)
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		///</summary>
		public bool Delete(Guid RecordId)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from [Sys_StatusRecord] ");
			strSql.Append(" where [RecordId]=@RecordId");

			SqlParameter[] parameters = {
			
				new SqlParameter("@RecordId", RecordId)             
			  
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Sys_StatusRecord GetAuditRecord(string RecordTable, string RecordType, string ObjectID)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("select  top 1 * from [Sys_StatusRecord] ");
			strSql.Append(" where RecordTable=@RecordTable and [RecordType]=@RecordType and [ObjectID] = @ObjectID ORDER BY CreateTime DESC");

			SqlParameter[] parameters = {
				new SqlParameter("RecordTable", RecordTable),
				new SqlParameter("RecordType", RecordType),
				new SqlParameter("ObjectID", ObjectID)  
			};

			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Sys_StatusRecord Get(Guid RecordId)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("select  top 1 * from [Sys_StatusRecord] ");
			strSql.Append(" where [RecordId]=@RecordId");

			SqlParameter[] parameters = {
			
				new SqlParameter("RecordId", RecordId)             
			  
			};

			DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Sys_StatusRecord Get(DataRow row)
		{
			if (row == null) return null;

			Sys_StatusRecord model = new Sys_StatusRecord();


			if (row["RecordId"] != DBNull.Value)
			{
				model.RecordId = (Guid)row["RecordId"];
			}

			if (row["MemberAccount"] != DBNull.Value)
			{
				model.MemberAccount = (String)row["MemberAccount"];
			}

			if (row["RecordType"] != DBNull.Value)
			{
				model.RecordType = (String)row["RecordType"];
			}

			if (row["BeforeValue"] != DBNull.Value)
			{
				model.BeforeValue = (Int32)row["BeforeValue"];
			}

			if (row["AfterValue"] != DBNull.Value)
			{
				model.AfterValue = (Int32)row["AfterValue"];
			}

			if (row["CreateTime"] != DBNull.Value)
			{
				model.CreateTime = (DateTime)row["CreateTime"];
			}

			if (row["Creator"] != DBNull.Value)
			{
				model.Creator = (String)row["Creator"];
			}

			if (row["Memo"] != DBNull.Value)
			{
				model.Memo = (String)row["Memo"];
			}

			if (row["ObjectID"] != DBNull.Value)
			{
				model.ObjectID = (String)row["ObjectID"];
			}

			if (row["LoginID"] != DBNull.Value)
			{
				model.LoginID = (Guid)row["LoginID"];
			}
			if (row["RecordTable"] != DBNull.Value)
			{
				model.RecordTable = (String)row["RecordTable"];
			}

			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet Gets(string sqlWhere)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("select * from [Sys_StatusRecord] ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}

			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet Gets(int Top, string sqlWhere, string filedOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select ");
			if (Top > 0)
			{
				strSql.Append(" top " + Top.ToString());
			}
			strSql.Append(" *  FROM [Sys_StatusRecord] ");

			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}
			if (filedOrder.Trim() != "")
			{
				strSql.Append(" order by " + filedOrder);
			}

			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string sqlWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) FROM [Sys_StatusRecord] ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 获得数据列表分页
		/// </summary>
		public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append(" SELECT * FROM [Sys_StatusRecord]  ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}

			int count = 0;
			DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by RecordId desc ", out count, cmdParms);
			total = count;
			return dt;
		}
		#endregion  成员方法
	}
}


